"""
__Title__ = 'example.py'
__Author__ = 'Administrator'
__Date__ = '2020/11/29'
__Version__ = 'Python 3.7.9'
__Software__ = 'PyCharm'
"""
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


class Table(QWidget):
    def __init__(self):
        super(Table, self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle("QTableWidget例子")
        self.resize(400, 300)
        layout = QHBoxLayout()
        # 设置一个4行3列的表格
        tableWidget = QTableWidget(4, 3)
        # 设置标题
        tableWidget.setHorizontalHeaderLabels(['姓名', '性别', '体重（kg）'])
        # 添加数据
        item = QTableWidgetItem('王者荣耀')
        # 设置名字不可编辑
        item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
        tableWidget.setItem(0, 0, item)

        item = QTableWidgetItem('男')
        tableWidget.setItem(0, 1, item)

        item = QTableWidgetItem('170')
        tableWidget.setItem(0, 2, item)

        # 添加tableWidget到水平布局中
        layout.addWidget(tableWidget)
        # 应用水平布局
        self.setLayout(layout)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Table()
    win.show()
    sys.exit(app.exec_())